package com.ts.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ts.core.client.OssClient;
import com.ts.core.entity.AndroidVo.AnArticleVo;
import com.ts.core.entity.AndroidVo.AnPostArticleVo;
import com.ts.core.entity.LxbArticle;
import com.ts.core.entity.LxbBlogger;
import com.ts.core.entity.LxbUser;
import com.ts.core.entity.LxbVideo;
import com.ts.core.entity.Vo.ArticleVo;
import com.ts.core.entity.Vo.DetailVo;
import com.ts.core.entity.queryVo.ArticleQueryVo;
import com.ts.core.mapper.LxbArticleMapper;
import com.ts.core.mapper.LxbBloggerMapper;
import com.ts.core.mapper.LxbUserMapper;
import com.ts.core.mapper.LxbVideoMapper;
import com.ts.core.service.LxbArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 帖子 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2024-03-25
 */
@Service
public class LxbArticleServiceImpl extends ServiceImpl<LxbArticleMapper, LxbArticle> implements LxbArticleService {
    @Resource
    private LxbBloggerMapper lxbBloggerMapper;

    @Resource
    private LxbVideoMapper lxbVideoMapper;

    @Resource
    private LxbUserMapper lxbUserMapper;

    @Resource
    private OssClient ossClient;

    @Override
    public List<ArticleVo> getAll() {
        List<ArticleVo> articleVoList=new ArrayList<>();
        List<LxbArticle> lxbArticles = baseMapper.selectList(null);
        for(LxbArticle article:lxbArticles)
        {
            ArticleVo articleVo = new ArticleVo();
            BeanUtils.copyProperties(article,articleVo);


            LxbBlogger lxbBlogger = lxbBloggerMapper.selectById(article.getBloggerId());

            if (!(lxbBlogger ==null)) {
                articleVo.setName(lxbBlogger.getName());
                articleVoList.add(articleVo);
            }






        }
        return articleVoList;
    }

    @Override
    public List<ArticleVo> selectList(ArticleQueryVo articleQueryVo) {
        QueryWrapper<LxbArticle> wrapper = new QueryWrapper<>();

        //根据博主名字 查询
        String name = articleQueryVo.getName();
        String idByName = baseMapper.getIdByName(name);
        wrapper.eq("blogger_id",idByName);


        if (!StringUtils.isEmpty(articleQueryVo.getTitle())) {
            wrapper.eq("title",articleQueryVo.getTitle());
        }
        if (!StringUtils.isEmpty(articleQueryVo.getState())) {
            wrapper.eq("state",articleQueryVo.getState());
        }
        if(!StringUtils.isEmpty(articleQueryVo.getLikes()))
        {
            wrapper.ge("likes",articleQueryVo.getLikes());
        }
        if(!StringUtils.isEmpty(articleQueryVo.getBrowse()))
        {
            wrapper.ge("browse",articleQueryVo.getBrowse());
        }

        if(!StringUtils.isEmpty(articleQueryVo.getTime()))
        {
            try {
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                Date begintime = df.parse(articleQueryVo.getTime());

                //Java日期提前几天(日期加)
                Calendar cal = Calendar.getInstance();
                cal.setTime(begintime);
                cal.add(Calendar.DAY_OF_MONTH, 1);//加1天
                Date endtime = cal.getTime();
                wrapper.ge("gmt_create",begintime);
                wrapper.lt("gmt_create", endtime);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }


        List<LxbArticle> articleList = baseMapper.selectList(wrapper);
        List<ArticleVo> articleVoList=new ArrayList<>();
        for(LxbArticle article:articleList)
        {
            ArticleVo articleVo = new ArticleVo();
            BeanUtils.copyProperties(article,articleVo);


            LxbBlogger lxbBlogger = lxbBloggerMapper.selectById(article.getBloggerId());

            if (!(lxbBlogger ==null)) {
                articleVo.setName(lxbBlogger.getName());
                articleVoList.add(articleVo);
            }






        }



        return articleVoList;
    }

    @Override
    public void updateStateById(String id) {
        LxbArticle lxbArticle = baseMapper.selectById(id);
        lxbArticle.setState(1);
        baseMapper.updateById(lxbArticle);
    }

    @Override
    public DetailVo getDetail(String id) {
        DetailVo detailVo = new DetailVo();
        LxbArticle lxbArticle = baseMapper.selectById(id);
        detailVo.setTitle(lxbArticle.getTitle());
        detailVo.setContent(lxbArticle.getIntro());
        detailVo.setState(lxbArticle.getState());
        detailVo.setLikes(lxbArticle.getLikes());
        detailVo.setBrowse(lxbArticle.getBrowse());


        String bloggerId = lxbArticle.getBloggerId();
        LxbBlogger lxbBlogger = lxbBloggerMapper.selectById(bloggerId);
        detailVo.setName(lxbBlogger.getName());
        detailVo.setIntro(lxbBlogger.getIntro());
        detailVo.setAvatar(lxbBlogger.getAvatar());
        detailVo.setCareer(lxbBlogger.getCareer());
        detailVo.setLevel(lxbBlogger.getLevel());





        QueryWrapper<LxbVideo> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("article_id",lxbArticle.getId());
        List<LxbVideo> videoList = lxbVideoMapper.selectList(videoQueryWrapper);



        int size = videoList.size();
        detailVo.setNum(size);
//
//        List<LxbVideo> videos=new ArrayList<>();
//        for(int i=0;i<size;i++)
//        {
//            videos.add(videoList.get(i));
//        }
        detailVo.setVideoList(videoList);






        return detailVo;
    }

    @Override
    public List<AnArticleVo> getAllArticle() {
        List<AnArticleVo> anArticleVos = new ArrayList<>();


        //查询所有审核通过的帖子
        QueryWrapper<LxbArticle> lxbArticleQueryWrapper = new QueryWrapper<>();
        lxbArticleQueryWrapper.eq("state",1);
        List<LxbArticle> lxbArticleList = baseMapper.selectList(lxbArticleQueryWrapper);

        for (int i = 0; i < lxbArticleList.size(); i++) {
            AnArticleVo anArticleVo = new AnArticleVo();
            LxbArticle lxbArticle = lxbArticleList.get(i);
            BeanUtils.copyProperties(lxbArticle,anArticleVo);

            String bloggerId = lxbArticle.getBloggerId();//获取发帖人的昵称和头像
            LxbBlogger lxbBlogger = lxbBloggerMapper.selectById(bloggerId);

            anArticleVo.setName(lxbBlogger.getName());
            anArticleVo.setAvatar(lxbBlogger.getAvatar());

            //根据id获取帖子的图片&视频
            QueryWrapper<LxbVideo> videoQueryWrapper = new QueryWrapper<>();
            videoQueryWrapper.eq("article_id",lxbArticle.getId());
            videoQueryWrapper.orderByAsc("id");
            List<LxbVideo> videos = lxbVideoMapper.selectList(videoQueryWrapper);
            if(videos.size()>0){

                LxbVideo lxbVideo = videos.get(0);
                String s = lxbVideo.getVideoOriginalName().replace("\r", "").replace("\n", "");
                //通过远程服务调用获取帖子的封面照片
                System.out.println(s);

                String imageUrl = ossClient.getImageUrl(s);
                anArticleVo.setImage(imageUrl);
                for (int j = 0; j < videos.size(); j++) {
                    LxbVideo lxbVideo1 = videos.get(j);
                    String url = ossClient.getImageUrl(lxbVideo1.getVideoOriginalName().replace("\r", "").replace("\n", ""));
                    lxbVideo1.setVideoOriginalName(url);
                }


                anArticleVo.setVideoList(videos);
            }




            anArticleVos.add(anArticleVo);

        }


        return anArticleVos;
    }

    @Override
    public void CreateArticleAndVideo(AnPostArticleVo anArticleVo) {
        //根据账号获取BloggerId
        String id = anArticleVo.getId();
        QueryWrapper<LxbUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account",id);
        LxbUser lxbUser = lxbUserMapper.selectOne(queryWrapper);



        LxbArticle lxbArticle = new LxbArticle();
        lxbArticle.setBloggerId(lxbUser.getBloggerId());
        lxbArticle.setTitle(anArticleVo.getTitle());
        lxbArticle.setIntro(anArticleVo.getIntro());
        lxbArticle.setState(1);
        baseMapper.insert(lxbArticle);



        LxbVideo lxbVideo = new LxbVideo();
        lxbVideo.setArticleId(lxbArticle.getId());
        lxbVideo.setVideoOriginalName(anArticleVo.getVideoOriginalName());
        lxbVideo.setVideoSourceId(anArticleVo.getVideoSourceId());
        lxbVideoMapper.insert(lxbVideo);



    }
}
